VB Sample Code for modifying group conditions

Private Sub mnuEditGroupConditions_Click()
Dim result%, jobnum%, mainjob%, groupcount%, groupN%, fieldHandle&, fieldLength%, Condition%, Direction%
Dim FieldName$, TempText$

If Not CRYSTAL_PRO Then
MsgBox "Modifying Group Conditions is a Crystal Pro only feature.", vbOKOnly + vbCritical, "Crystal Pro Only"
Exit Sub
End If

result% = PEOpenEngine()
If result% = 0 Then
MsgBox "Could not start the report engine. Execution must halt.", vbOKOnly + vbCritical, "Serious Error"
End
End If

jobnum% = PEOpenPrintJob(lblReportName.Caption) ' Name from label on sample form
ErrorTrap "OpenPrintJob in EditGroupCondition", jobnum%

' Subreport check - if a subreport is currently selected on the main form, jobnum% becomes the subreport
If lblSubreportName.Visible Then
mainjob% = jobnum%
jobnum% = PEOpenSubreport(mainjob%, lblSubreportName.Caption)
ErrorTrap "OpenSubReport in EditGroupCondition", mainjob%
End If

' Count how many groups there are
groupcount% = PEGetNGroups(jobnum%)
If groupcount% = -1 Then
' Something went wrong
ErrorTrap "GetNGroups in EditGroupCondition", jobnum%
ElseIf groupcount% = 0 Then
' None!
MsgBox "There are no groups defined in this report.", vbOKOnly + vbCritical, "No Groups"
Else
' There are groups
' The Group Conditions form is loaded to display the various conditions
Load GroupConditions
CenterForm Sample, GroupConditions

' Loop through the number of sort fields determined by PEGetNSortFields
For groupN% = 0 To groupcount% - 1
' Get the fieldhandle, length and sort direction
result% = PEGetGroupCondition(jobnum%, groupN%, fieldHandle&, fieldLength%, Condition%, Direction%)
ErrorTrap "GetGroupCondition in EditGroupCondition", jobnum%
' Set chr$(0) into the fieldname string based on the length retrieved in the preceding call
' NOTE! Failure to do this step results in GPFs
FieldName$ = String$(fieldLength%, 0)
' crvbHandleToBstr fills the fieldname string with the name of the sort field
result% = crvbHandleToBstr(fieldHandle&, FieldName$, fieldLength%)
ErrorTrap "HandleToBstr for FieldName in EditGroupCondition", jobnum%
' The fieldname string is then inserted into the list box on the sort order form
GroupConditions!lstFieldName.AddItem FieldName$
' To determine what condition text should be displayed in the list box, a select case statement
' using a mask determines what type of condition type, then the condition itself
Select Case Condition% And PE_GC_TYPEMASK
Case PE_GC_TYPEOTHER
' Others have no conditions
TempText$ = ""
Case PE_GC_TYPEDATE
Select Case Condition% And PE_GC_CONDITIONMASK
Case PE_GC_DAILY
TempText$ = "Daily"
Case PE_GC_WEEKLY
TempText$ = "Weekly"
Case PE_GC_BIWEEKLY
TempText$ = "BiWeekly"
Case PE_GC_SEMIMONTHLY
TempText$ = "SemiMonthly"
Case PE_GC_MONTHLY
TempText$ = "Monthly"
Case PE_GC_QUARTERLY
TempText$ = "Quarterly"
Case PE_GC_SEMIANNUALLY
TempText$ = "SemiAnnually"
Case PE_GC_ANNUALLY
TempText$ = "Annually"
End Select
Case PE_GC_TYPEBOOLEAN
Select Case Condition% And PE_GC_CONDITIONMASK
Case PE_GC_TOYES
TempText$ = "To Yes"
Case PE_GC_TONO
TempText$ = "To No"
Case PE_GC_EVERYYES
TempText$ = "Every Yes"
Case PE_GC_EVERYNO
TempText$ = "Every No"
Case PE_GC_NEXTISYES
TempText$ = "Next Is Yes"
Case PE_GC_NEXTISNO
TempText$ = "Next Is No"
End Select
End Select
GroupConditions!lstCondition.AddItem TempText$
' Store the condition value in the item data in the list box
GroupConditions!lstCondition.ItemData(GroupConditions!lstFieldName.NewIn dex) = Condition%
' Load direction into the direction list box
If Direction% = PE_SF_DESCENDING Then
TempText$ = "Des"
Else
TempText$ = "Asc"
End If
GroupConditions!lstDirection.AddItem TempText$
Next groupN%

' Once the list is complete, the sort order form is shown 1 ' Modally - execution in this module will stop
' until the sort order form is hidden (by pressing Ok or Cancel)
GroupConditions.Show 1 ' Modal

' Now that execution has returned, check what button was pressed and react accordingly
Select Case GroupConditions.Tag
Case "Ok"
If MsgBox("Do you want to change the group conditions of this print job?", vbYesNo + vbQuestion, "Change Group Conditions?") = vbYes Then
' Make changes to the print job group conditions - note that this has no permanent affect on the report
' Changes made at the API level cannot be stored in the report
For groupN% = 0 To GroupConditions!lstFieldName.ListCount - 1
' Copy the name of the sort field from the list box
FieldName$ = GroupConditions!lstFieldName.List(groupN%) & Chr$(0)
Condition% = GroupConditions!lstCondition.ItemData(groupN%)
If GroupConditions!lstDirection.List(groupN%) = "Des" Then
Direction% = PE_SF_DESCENDING
Else
Direction% = PE_SF_ASCENDING
End If
result% = PESetGroupCondition(jobnum%, groupN%, FieldName$, Condition%, Direction%)
ErrorTrap "GetGroupCondition in EditGroupCondition", jobnum%
Next groupN%
End If
Case "Cancel"
MsgBox "Cancel was pressed on the Group Conditions form. The group condition of the print job will not be changed.", vbOKOnly + vbInformation, "Cancel Pressed"
End Select

' Finished with the sort order form - unload it
Unload GroupConditions

' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
' Simplified version of the custom-link preview routine (no custom buttons)
result% = PEOutputToWindow(jobnum%, "Sort Order Demonstration Preview" & Chr$(0), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0)
ErrorTrap "OutputtoWindow in EditGroupCondition", jobnum%

result% = PEStartPrintJob(jobnum%, True)
ErrorTrap "StartPrintJob in EditGroupCondition", jobnum%

result% = 1
Do While result% <> 0
DoEvents
DoEvents
result% = PEGetWindowHandle(jobnum%)
Loop
End If
End If

' Close print job and engine
' Subreport check - if a subreport is currently open, call PreviewReport to offer a chance to
' preview the main report, then close the subreport and main report
If lblSubreportName.Visible Then
PreviewReport mainjob%
result% = PECloseSubreport(jobnum%)
ErrorTrap "CloseSubReport in EditGroupCondition", mainjob%
PEClosePrintJob mainjob%
Else
PEClosePrintJob jobnum%
End If

PECloseEngine

MsgBox "Group Conditions Complete!", vbOKOnly, "Operation Succeeded"

End Sub
ActiveX
Private Sub mnuEditGroupConditions_Click()
Dim groupN As Integer
Dim GroupName As String, FieldName As String, Condition As String, SortDirection As String
Dim hwndPreviewWindow As Long

If Not CRYSTAL_PRO Then
MsgBox "Modifying Group Conditions is a Crystal Pro only feature.", vbOKOnly + vbCritical, "Crystal Pro Only"
Exit Sub
End If

CrystalReport1.ReportFileName = lblReportName.Caption ' Name from label on sample form

' Set up endless loop (only ends with an Exit Do) for editing multiple group conditions
Do While True
GroupName = InputBox("Enter group condition to edit. Press Cancel to end editing of group conditions:", "Enter Group Condition Number", Str$(groupN))
' if a zero length string, then Cancel was pressed, exit the loop
If GroupName = "" Then Exit Do
' Otherwise a group condition number was entered, convert it
groupN = Val(GroupName)
' Groupname must be preceded by the word group
GroupName = "GROUP" & GroupName
' Get FieldName
FieldName = InputBox("Enter field name for group condition. Press Cancel to end editing of group conditions. Field names must be encased in braces:", "Enter Field Name:", "{}")
' if a zero length string, then Cancel was pressed, exit the loop
If FieldName = "" Then Exit Do
' Get Condition
Condition = InputBox("Enter condition code for grouping. If field is a date or boolean field, please refer to the help file for conditions, for all other fields, leave set to default (ANYCHANGE):", "Enter Condition", "ANYCHANGE")
If Condition = "" Then Exit Do
' Get Sort Direction
SortDirection = InputBox("Enter the sort direction. Press Cancel to end editing of group conditions. Press 'A' for Ascending, 'D' for Descending.", "Enter Sort Direction", "A")
If SortDirection = "" Then Exit Do
' Load Group Condition with data input
CrystalReport1.GroupCondition(groupN) = GroupName & FieldName & Condition & SortDirection
groupN = groupN + 1
Loop

' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
CrystalReport1.Destination = 0 ' Window
CrystalReport1.Action = 1 ' Print
ErrorTrap "GroupConditions"
hwndPreviewWindow = GetActiveWindow()
Do While IsWindow(hwndPreviewWindow)
DoEvents
Loop
End If

' Close the report
CrystalReport1.ReportFileName = ""

MsgBox "Group Conditions Complete!", vbOKOnly, "Operation Completed"

End Sub


Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com